<?php

namespace App\Api\Controllers;

use Auth;
use App\Models\User;
use Illuminate\Http\Request;
use App\Api\Requests\AuthorizationRequest;

class AuthorizationsController extends Controller
{
    public function store(AuthorizationRequest $request)
    {
        $credentials['mobile'] = $request->mobile;
        $credentials['password'] = $request->password;

        if (!$token = Auth::guard('api')->attempt($credentials)) {
            return $this->error('用户名或密码错误');
        }

        return $this->respondWithToken($token)->setStatusCode(200);
    }

    public function update()
    {
        $token = Auth::guard('api')->refresh();
        return $this->respondWithToken($token);
    }

    public function destroy()
    {
        Auth::guard('api')->logout();
        return $this->success();
    }

    protected function respondWithToken($token)
    {
        return $this->success([
            'access_token' => $token,
            'token_type' => 'Bearer',
            'expires_in' => Auth::guard('api')->factory()->getTTL() * 60000
        ]);
    }
}